package de.homelabs.homelabs.plugin.wunderground;

import java.text.SimpleDateFormat;

import org.springframework.stereotype.Service;

import de.mbenning.weather.wunderground.api.domain.DataSet;
import de.mbenning.weather.wunderground.api.domain.HttpProxy;
import de.mbenning.weather.wunderground.api.domain.WeatherStation;
import de.mbenning.weather.wunderground.api.domain.WeatherStations;
import de.mbenning.weather.wunderground.impl.services.HttpDataReaderService;

@Service("hlWundergroundService")
public class HLWundergroundService {

	public StringBuffer getWunderGroundData(){
		//var 
		StringBuffer buffer = new StringBuffer();
		SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy H:mm:s");
		
		// create a instance of a wunderground data reader
		HttpDataReaderService httpDataReaderService = new HttpDataReaderService();
		httpDataReaderService.setHttpProxy(new HttpProxy());

		// select a wunderground weather station (ID "INORDRHE72" = Dortmund-Mengede)
		WeatherStation weatherStation = WeatherStations.ALL.get("INORDRHE72");

		// set selected weather station to data reader
		httpDataReaderService.setWeatherStation(weatherStation);

		// get current (last) weather data set from selected station
		DataSet current = httpDataReaderService.getCurrentData();
		buffer.append("<p>");
		buffer.append("<b>Wunderground-core Widget (<a href='http://code.google.com/p/wunderground-core/'>http://code.google.com/p/wunderground-core/</a> by m.benning)</b><br /><br />");
		// print selected weather station ID
		buffer.append("Stations-ID: "+weatherStation.getStationId() + "<br />");

		// print city, state and country of weather station
		buffer.append("Stadt: "+ weatherStation.getCity() + "<br />Land: " + weatherStation.getState() + "<br />Stadt: " + weatherStation.getCountry()+ "<br />");

		// print datetime of measure and temperature ...
		buffer.append("Datum: "+sdf.format(current.getDateTime()) + "<br />Temperatur: " + current.getTemperature()+ " Grad <br />");
		buffer.append("</p>");
		//return
		return buffer;
	}
}
